package Application;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;

import Application.AppStates.AppStatesHandler;
import Application.Components.DiningList.DiningList;
import Application.Components.ImageShow.ImageShowManager;
import Application.Components.Schedule.Schedule;
import Application.Window.Window;

/**
 * Application class
 * Running in separated thread, handling application states (images, schedule etc)
 * 
 * @author drahomir.karchnak
 *
 */

public class Application extends TimerTask{
	private Timer timer;
	
	private ServerHandler serverHandler;
	private AppStatesHandler appStatesHandler;
	
	private Window window;
	private Toolkit toolkit;
	
	private Schedule	schedule;
	private ImageShowManager	imageShowManager;
	private DiningList	diningList;
	
	/**
	 * Constructor for Application
	 * 	getting pointer to ServerHandler (can get data from)
	 * 	@param serverHandler
	 */
	
	public Application(ServerHandler serverHandler){
		this.appStatesHandler	= new AppStatesHandler();
		this.serverHandler		= serverHandler;
		
		System.out.println("[i] Application Thread is running!");
		
		this.toolkit = Toolkit.getDefaultToolkit();
		Dimension dim = toolkit.getScreenSize();
		
		//dim.setSize(1280, 720);
		
		schedule = new Schedule(serverHandler, dim.width, dim.height);
		imageShowManager = new ImageShowManager(serverHandler, dim.width, dim.height);
		diningList = new DiningList(dim.width, dim.height);
		
		
		this.window = new Window(serverHandler, this, appStatesHandler,dim.width, dim.height, schedule, imageShowManager, diningList);
		
		timer = new Timer();
		timer.schedule(this,	appStatesHandler.STATE_CHANGE_DELAY, 
								appStatesHandler.STATE_CHANGE_DELAY);
	}
	
	/**
	 * This method runs every appStateHandler.STATE_CHANGE_DELAY ms
	 */
	
	@Override
	public void run() {	
		appStatesHandler.nextAppState();
		window.paint(window.getGraphics());
	}

	public void stop(){
		timer.cancel();
	}
}
